// Ninf-G-XX, ver. 0.1
// Copyright 2011 National Institute of Advanced Industrial Science and Technology
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdio.h>
#include <assert.h>
#include <ctime>
#include "Logger.hpp"

using namespace ninf::lib;
/** create Logger */
Logger :: Logger(const char * filename)
: logTag(1)
{
  FILE * fp0 = fopen("w+", filename);
  assert (fp0 != NULL);
  fp = fp0;
}

/** create Logger */
Logger :: Logger(FILE * fp)
: logTag(1)
{
  this->fp = fp;
}

/** create Logger with stderr*/
Logger :: Logger() 
: logTag(1)
{
  fp = stderr;
}

/** set tag as logTag */
void 
Logger :: setLogTag(int tag){
  logTag = tag;
}

/** add the tag as logTag */
void 
Logger :: addLogTag(int tag){
  logTag &= tag;
}


/** print out log */
void 
Logger :: log(int tag, const char * fmt, va_list list){
  if ((tag & logTag) != 0){
    time_t clock;
    char * pc;
    clock = time(NULL);
    pc = ctime(&clock);
    *(pc+24) = '\0';
    fprintf(fp, "%s:\t", pc);

    vfprintf(fp, fmt, list);
    fprintf(fp, "\n");
  }
}


/** print out log with tag 1 */
void 
Logger :: log(const char * fmt, ...){
  va_list ap;
  va_start(ap, fmt);
  log(1, fmt, ap);
  va_end(ap);
}


/** print out log */
void 
Logger :: log(int tag, const char * fmt, ...){
  va_list ap;
  va_start(ap, fmt);
  log(tag, fmt, ap);
  va_end(ap);
}

/** print out log with tag 2 */
void 
Logger :: debug(const char * fmt, ...){
  va_list ap;
  va_start(ap, fmt);
  log(2, fmt, ap);
  va_end(ap);
}


